﻿using Core;
using Core.Comamad;
using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class frmMain : Form
    {
        ProxyQuanLi facade = new ProxyQuanLi();
        QuanLiDSHocPhan ql = new QuanLiDSHocPhan();
        int gvSinhVienRow = -1;
        string malop = "";
        public frmMain()
        {
            InitializeComponent();
            dgvQLSV.AutoGenerateColumns = false;
            dgvLop.AutoGenerateColumns = false;
            //dgvQLHP.AutoGenerateColumns = false;
            //dgvQLDKHP.AutoGenerateColumns = false;
        }


        private void LoadDataHP(string path)
        {
            List<BaseData> Ds = DataSource.Instance.ReadHocPhan(path);
            ql.ListHocPhan = Ds;

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //facade.QLyLop = new QuanLyLop();

            //LoadData();
        }
        #region Load Data to GirdView
        public void LoadData()
        {
            LoadLop();
            LoadQLSV();

            // LoadDKHP();

        }
        private void LoadQLSV()
        {
            List<SinhVien> dssv = new List<SinhVien>();
            foreach (var item in ProxyQuanLi.Instance.QLSV.data)
            {

                dssv.Add(item as SinhVien);
            }
            dgvQLSV.DataSource = dssv;
        }
        private void LoadHP(List<BaseData> ds)
        {
            List<HocPhan> data = new List<HocPhan>();
            foreach (var item in ds)
            {

                data.Add(item as HocPhan);
            }
            dgvQLHP.DataSource = data;
        }
        private void LoadDKHP()
        {
            List<DangKiHocPhan> data = new List<DangKiHocPhan>();
            foreach (var item in facade.QLHocPhan.ListHocPhan)
            {

                data.Add(item as DangKiHocPhan);
            }
            dgvQLDKHP.DataSource = data;
        }
        private void LoadLop()
        {
            List<LopHoc> data = new List<LopHoc>();
            foreach (var item in ProxyQuanLi.Instance.QLyLop.data)
            {
                data.Add(item as LopHoc);
            }
            dgvLop.DataSource = data;
        }
        #endregion
        private void quảnLýSinhVienToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                var dialog = new OpenFileDialog();
                dialog.Multiselect = false;
                dialog.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt|Data files (*.dat)|*.dat";
                dialog.InitialDirectory = Application.ExecutablePath;
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    LoadDataHP(dialog.FileName);
                    LoadHP(ql.ListHocPhan);

                }

            }
            catch
            {

                MessageBox.Show("File bạn chọn không đúng cấu trúc", "Lỗi");
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (File.Exists("Dshp.txt"))
            {
                DialogResult dg = MessageBox.Show("file đã có bạn thật sự muốn lưu thay đổi không", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dg == DialogResult.OK)
                {
                    DataSource.Instance.WriteTo(ql.ToString(), "Dshp.txt");
                    QuanLiDSHocPhan.Instance.getHocPhan();
                }


            }
            else DataSource.Instance.WriteTo(ql.ToString(), "Dshp.txt");
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 3:
                    LoadLop();

                    break;
                case 1:
                    LoadHP(facade.QLHocPhan.ListHocPhan);
                    break;
                default:
                    break;
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialog = new SaveFileDialog();
            dialog.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt|Data files (*.dat)|*.dat";
            dialog.InitialDirectory = Application.ExecutablePath;
            if (dialog.ShowDialog(this) == DialogResult.OK)
                DataSource.Instance.WriteTo(ql.ToString(), dialog.FileName);
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {

            QuanLyLop.Instance.Undo();
            DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(), "DsLop.txt");
            LoadData();

        }


        int RowCuurent = -1;
        private void xóaLớpHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (RowCuurent != -1)
            {
                try
                {
                    DialogResult kt = MessageBox.Show("Bạn muốn xóa không", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (kt == DialogResult.Yes)
                    {
                        string id = dgvLop[0, RowCuurent].Value + "";
                        BaseData temp = QuanLyLop.Instance.Find(x => (x as LopHoc).MaLop == id);
                        QuanLyLop.Instance.Delete(temp);
                        DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(), "DsLop.txt");
                        LoadData();
                    }


                }
                catch (Exception)
                {

                    MessageBox.Show("lỗi không xóa được ");
                }
            }
            else MessageBox.Show("Chưa Chọn Danh Sách Lớp");
        }

        private void thêmLớpHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThemLop fr = new FormThemLop();
            fr.FormClosed += fr_FormClosed;
            fr.Show();
        }

        void fr_FormClosed(object sender, FormClosedEventArgs e)
        {

            LoadData();
        }

        private void dgvLop_SelectionChanged(object sender, EventArgs e)
        {
            RowCuurent = dgvLop.CurrentRow.Index;
        }

        private void cậpNhậtLớpHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                FormThemLop frs = new FormThemLop();
                FormThemLop.malop = dgvLop[0, RowCuurent].Value + "";
                frs.FormClosed += frs_FormClosed;
                frs.ShowDialog();
            }
            catch (Exception)
            {
                
                 
            }
      
        }

        void frs_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadData();
        }

        private void btnrefhes_Click(object sender, EventArgs e)
        {
            cblop.Items.Clear();
            foreach (var item in QuanLyLop.Instance.data)
            {
                cblop.Items.Add(item as LopHoc);

            }
            cblop.ValueMember = "MaLop";
            cblop.DisplayMember = "TenLop";
            cblop.Items.Insert(0, "--Chọn Lớp--");
            cblop.SelectedIndex = 0;
        }

        private void btnload_Click(object sender, EventArgs e)
        {
            List<BaseData> ds = QuanLiSinhVien.Instance.FindAll(x => (x as SinhVien).MaLop == malop);
            List<SinhVien> lis = new List<SinhVien>();
            foreach (var item in ds)
            {
                lis.Add(item as SinhVien);
            }
            LoadSVbyLop(lis);

        }

        private void LoadSVbyLop(List<SinhVien> lis)
        {
            dgvQLSV.DataSource = lis;
        }

        private void cblop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cblop.SelectedIndex > 0)
                malop = (cblop.SelectedItem as LopHoc).MaLop;
        }

        private void thêmSinhViênToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                var dialog = new OpenFileDialog();
                dialog.Multiselect = false;
                dialog.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt|Data files (*.dat)|*.dat";
                dialog.InitialDirectory = Application.ExecutablePath;
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    List<BaseData> ds = DataSource.Instance.ReadSinhVien(dialog.FileName);
                    QuanLiSinhVien.Instance.Add(ds);
                    DialogResult rs = MessageBox.Show("bạn có muốn thêm vào list sinh viên không", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (rs == DialogResult.Yes)
                    {
                        DataSource.Instance.WriteTo(QuanLiSinhVien.Instance.ToString(), "DsSinhVien.txt");
                        LoadLop();
                        DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(), "DsLop.txt");
                    }

                }

      

            }
            catch
            {

                MessageBox.Show("File bạn chọn không đúng cấu trúc", "Lỗi");
            }
        }

        private void dgvQLSV_SelectionChanged(object sender, EventArgs e)
        {
            gvSinhVienRow =  dgvQLSV.CurrentRow.Index;
        }

        private void xóaSinhViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string masv = dgvQLSV[0, gvSinhVienRow].Value + "";
                BaseData sv = QuanLiSinhVien.Instance.Find(x => (x as SinhVien).MSSV == masv);
                if (sv != null)
                {
                    DialogResult rs = MessageBox.Show("bạn muốn xóa sinh viên này không","",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
                    if (rs == DialogResult.OK)
                    {
                        QuanLiSinhVien.Instance.Delete(sv);
                        DataSource.Instance.WriteTo(QuanLiSinhVien.Instance.ToString(), "DsSinhVien.txt");
                        List<BaseData> gf = QuanLiSinhVien.Instance.FindAll(x => (x as SinhVien).MaLop == (sv as SinhVien).MaLop);
                        List<SinhVien> dshithi = new List<SinhVien>();
                        foreach (var item in gf)
                        {
                            dshithi.Add(item as SinhVien);
                        }
                        LoadSVbyLop(dshithi);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("không xóa được");
                 
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuanLiSinhVien.Instance.setSiSo();
            LoadLop();
            DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(),"DsLop.txt");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void undoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            QuanLyLop.Instance.Undo();
            LoadLop();
            
        }


    }
}
